package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketBrandService;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import static com.cskaoyan.market.util.GetReqBodyUtil.getRequestBody;

/**
 * @NAME: BrandController
 * @DESCRIPTION: 品牌制造商controller层
 * @DATE: 2023/5/7 15:32
 * @AUTHOR: 刘超
 */
@RestController
@RequestMapping("/admin/brand")
@MultipartConfig
public class AdminBrandController{
    @Autowired
    MarketBrandService marketBrandService;


    /**
     * 查询品牌制造商
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        if (page == null) {
            page = "1";
        }
        if (limit == null) {
            limit = "10";
        }
        if (sort == null) {
            sort = "id";
        }
        if (order == null) {
            order = "desc";
        }
        List<MarketBrand> marketBrands;
        if (StringUtils.isEmpty(id)) {
            if (StringUtils.isEmpty(name)){
                marketBrands = marketBrandService.list(page, limit, sort, order);
            }else{
                marketBrands = marketBrandService.list(page, limit, sort, order,name);
            }
        } else {
            marketBrands = marketBrandService.list(Integer.parseInt(id));
        }

        resp.getWriter().println(JacksonUtil.
                getObjectMapper().writeValueAsString(ResponseUtil.okList(marketBrands)));


    }

    /**
     * 新建品牌制造商
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("create");
        // 读入请求参数
        String requestBody = getRequestBody(req);
        MarketBrand marketBrand = JacksonUtil.getObjectMapper().readValue(requestBody, MarketBrand.class);
        // 检查参数正确性
        if (StringUtils.isEmpty(marketBrand.getName())||StringUtils.isEmpty(marketBrand.getPicUrl())) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        // 调用service层接口
        marketBrand.setSortOrder((byte) 1);
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        MarketBrand resMarketBrand = marketBrandService.create(marketBrand);
        // 封装对象，准备返回
        respMarketBrand(resp, resMarketBrand);

    }


    /**
     * 修改品牌制造商信息
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 读入请求参数
        String requestBody = getRequestBody(req);
        MarketBrand marketBrand = JacksonUtil.getObjectMapper().readValue(requestBody, MarketBrand.class);
        // 检验参数正确性
        if (marketBrand.getId() == null || StringUtils.isEmpty(marketBrand.getName())) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        marketBrand.setUpdateTime(LocalDateTime.now());
        // 调用service层接口
        MarketBrand respMarketBrand = marketBrandService.update(marketBrand);
        // 封装对象，准备返回
        respMarketBrand(resp, respMarketBrand);
    }

    /**
     * 删除品牌制造商
     *
     * @param req
     * @param resp
     */
    @RequestMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("delete");
        // 读入请求参数
        String requestBody = getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");

        if (StringUtils.isEmpty(id)) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // code 0:成功 1:失败

        int code = marketBrandService.delete(id);
        if (code == 0) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
            return;
        }
        // 删除失败
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgumentValue()));

    }

    /**
     * 封装MarketBrand对象，准备返回
     *
     * @param resp
     * @param marketBrand
     * @throws IOException
     */
    private static void respMarketBrand(HttpServletResponse resp, MarketBrand marketBrand) throws IOException {
        if (marketBrand != null) {

            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketBrand)));
        } else {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
        }
    }


}
